import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PictureUtil {
//    private Graphics2D g        = null;

    /**
     * 导入本地图片到缓冲区
     */
    public static BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public static BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
        BufferedImage newBufferedImage = null;
        try {
            int w = b.getWidth();
            int h = b.getHeight()+d.getHeight();
//            newBufferedImage = new BufferedImage(w,h, Transparency.TRANSLUCENT);
            newBufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
            Graphics2D g = newBufferedImage.createGraphics();
            g.fillRect(0, 0, w, b.getHeight());//解决背景黑色问题
            g.drawImage(b, 0, 0, w, b.getHeight(), null);
            g.drawImage(d, 0, b.getHeight(), d.getWidth(), d.getHeight(), null);
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return newBufferedImage;
    }

    public static BufferedImage modifyImage(BufferedImage b) {
        BufferedImage newBufferedImage = null;
        try {
            int w = b.getWidth();
            int h = b.getHeight();
            newBufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
            Graphics2D g = newBufferedImage.createGraphics();
            g.drawImage(b, 0, 0, w,120, null);
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return newBufferedImage;
    }

    /**
     * 生成新图片到本地
     */
    public static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static void mergerImage(String pictureInA, String pictureInB,String pictureOut) {
        BufferedImage imageInA = loadImageLocal(pictureInA);
        BufferedImage imageInB = loadImageLocal(pictureInB);
        writeImageLocal(pictureOut,modifyImagetogeter(imageInA, imageInB));
        //将多张图片合在一起
        System.out.println("success");
    }

    public static void main(String[] args) {

        PictureUtil tt = new PictureUtil();


        BufferedImage b = tt.loadImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\图片2.jpg");
        BufferedImage d = tt.loadImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\0.jpg");
//        BufferedImage d = tt.loadImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\excel.jpeg");
//        BufferedImage b = tt.loadImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\excel2.jpeg");

//        tt.writeImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\excel12.jpeg", tt.modifyImage(b));
        tt.writeImageLocal("E:\\workspace\\JavaDemo\\src\\main\\resources\\excel123.jpeg", tt.modifyImagetogeter(b, d));
        //将多张图片合在一起
        System.out.println("success");
    }
}
